package com.sfh.gd.config;

import com.sfh.gd.exception.BusinessException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
                // 从 Header 或 Session 中取 Token
        String token = request.getHeader("Authorization");

        // 简化示例：真实项目中请验证 JWT 或 Redis 登录态
        if (token == null || !"VALID_TOKEN".equals(token)) {
            log.warn("未登录访问被拦截: {}", request.getRequestURI());
            throw new BusinessException(100, "用户未登录，请先登录");
        }

        return true; // 登录通过
    }

}